home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / ComboBox.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-12-16  |  9.5 KB  |  552 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Event;
  8. import java.awt.Font;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.LayoutManager;
  12. import java.awt.Panel;
  13. import java.awt.TextField;
  14.  
  15. public class ComboBox extends Panel {
  16.    public static final int EVT_IMAGE_SELECT = 16388;
  17.    private boolean bEditable;
  18.    private boolean bSearchable;
  19.    private boolean bCaseSensitive;
  20.    private TextField editBox;
  21.    private DirectionButton arrow;
  22.    private ImageListBox list;
  23.    private boolean bDown;
  24.    // $FF: renamed from: ix int
  25.    private int field_0;
  26.    // $FF: renamed from: iy int
  27.    private int field_1;
  28.    private int iwidth;
  29.    private int iheight;
  30.    private int lheight;
  31.    private boolean bOverList;
  32.    private boolean showHScroll;
  33.    private boolean showVScroll;
  34.    private Font editFont;
  35.    private Font dropFont;
  36.    private static boolean bOsFlag;
  37.    private static ComboBox currentlyDown = null;
  38.    private Component parent;
  39.    private String sSearchText;
  40.    private String sSearchItem;
  41.    private String sLastText;
  42.    private String sSearchOrig;
  43.    private int searchIndex;
  44.    private int searchLen;
  45.    private int searchI;
  46.  
  47.    public ComboBox() {
  48.       this(false, false);
  49.    }
  50.  
  51.    public ComboBox(boolean var1, boolean var2) {
  52.       this.bDown = false;
  53.       this.bOverList = false;
  54.       this.showHScroll = false;
  55.       this.showVScroll = true;
  56.       bOsFlag = !System.getProperty("os.name").startsWith("Win");
  57.       ((Container)this).setLayout((LayoutManager)null);
  58.       this.editBox = new TextField("");
  59.       this.editBox.setBackground(Color.white);
  60.       this.arrow = new DirectionButton(3);
  61.       this.list = new ImageListBox("ILB");
  62.       this.list.setComboMode(true);
  63.       this.list.setShowHorizontalScroll(false);
  64.       ((Container)this).add(this.list);
  65.       ((Container)this).add(this.editBox);
  66.       ((Container)this).add(this.arrow);
  67.       this.arrow.setShowFocus(false);
  68.       this.arrow.shrinkTriangle(1, 1, 5, 5);
  69.       this.arrow.disable();
  70.       this.lheight = 0;
  71.       this.list.hide();
  72.       this.setEditable(var1);
  73.       this.setSearchable(var2);
  74.       this.bCaseSensitive = true;
  75.    }
  76.  
  77.    public void setEditable(boolean var1) {
  78.       this.bEditable = var1;
  79.       this.editBox.setEditable(this.bEditable || this.bSearchable);
  80.    }
  81.  
  82.    public boolean getEditable() {
  83.       return this.bEditable;
  84.    }
  85.  
  86.    public void setSearchable(boolean var1) {
  87.       this.bSearchable = var1;
  88.       this.editBox.setEditable(this.bEditable || this.bSearchable);
  89.    }
  90.  
  91.    public boolean getSearchable() {
  92.       return this.bSearchable;
  93.    }
  94.  
  95.    public void setCaseSensitive(boolean var1) {
  96.       this.bCaseSensitive = var1;
  97.    }
  98.  
  99.    public boolean getCaseSensitive() {
  100.       return this.bCaseSensitive;
  101.    }
  102.  
  103.    public void setShowHorizontalScroll(boolean var1) {
  104.       this.showHScroll = var1;
  105.       this.list.setShowHorizontalScroll(var1);
  106.    }
  107.  
  108.    public boolean getShowHorizontalScroll() {
  109.       return this.showHScroll;
  110.    }
  111.  
  112.    public void setShowVerticalScroll(boolean var1) {
  113.       this.showVScroll = var1;
  114.       this.list.setShowVerticalScroll(var1);
  115.    }
  116.  
  117.    public boolean getShowVerticalScroll() {
  118.       return this.showVScroll;
  119.    }
  120.  
  121.    public synchronized void setComboBoxFont(Font var1) {
  122.       this.setFont(var1);
  123.    }
  124.  
  125.    public synchronized Font getComboBoxFont() {
  126.       return this.editFont;
  127.    }
  128.  
  129.    public synchronized void setEditFieldFont(Font var1) {
  130.       if (this.editBox != null && var1 != null && this.iheight > 15) {
  131.          this.editFont = var1;
  132.          this.editBox.setFont(var1);
  133.       }
  134.  
  135.    }
  136.  
  137.    public synchronized Font getEditFieldFont() {
  138.       return this.editFont;
  139.    }
  140.  
  141.    public synchronized void setDropDownFont(Font var1) {
  142.       if (this.list != null && var1 != null) {
  143.          this.dropFont = var1;
  144.          this.list.setFont(var1);
  145.       }
  146.  
  147.    }
  148.  
  149.    public synchronized Font getDropDownFont() {
  150.       return this.dropFont;
  151.    }
  152.  
  153.    public boolean handleEvent(Event var1) {
  154.       switch (var1.id) {
  155.          case 401:
  156.             this.sLastText = this.editBox.getText();
  157.             break;
  158.          case 402:
  159.             if (var1.target == this.editBox) {
  160.                this.verify();
  161.             }
  162.             break;
  163.          case 404:
  164.             if (var1.target != this.editBox) {
  165.                switch (var1.key) {
  166.                   case 1000:
  167.                      this.list.select(0);
  168.                      break;
  169.                   case 1001:
  170.                      this.list.select(this.list.countItems() - 1);
  171.                      break;
  172.                   case 1002:
  173.                   case 1004:
  174.                      this.list.select(this.list.getSelectedIndex() - 1);
  175.                      break;
  176.                   case 1003:
  177.                   case 1005:
  178.                      this.list.select(this.list.getSelectedIndex() + 1);
  179.                }
  180.  
  181.                this.editBox.setText(this.list.getSelectedItem());
  182.                this.editBox.selectAll();
  183.             }
  184.             break;
  185.          case 504:
  186.             if (var1.target == this.list) {
  187.                this.bOverList = true;
  188.             }
  189.             break;
  190.          case 505:
  191.             if (var1.target == this.list) {
  192.                this.bOverList = false;
  193.             }
  194.             break;
  195.          case 701:
  196.             this.editBox.setText(this.list.getSelectedItem());
  197.             if (this.parent == null) {
  198.                this.parent = ((Component)this).getParent();
  199.             }
  200.  
  201.             if (this.parent != null) {
  202.                var1.target = this;
  203.                var1.id = 1001;
  204.                this.parent.postEvent(var1);
  205.             }
  206.  
  207.             this.dropList(false);
  208.             return true;
  209.          case 1001:
  210.             if (var1.target instanceof DirectionButton) {
  211.                this.dropList(!this.bDown);
  212.                return true;
  213.             }
  214.  
  215.             if (var1.target == this.list) {
  216.                return true;
  217.             }
  218.             break;
  219.          case 1004:
  220.             if (var1.target == this && !this.bDown) {
  221.                this.editBox.selectAll();
  222.                this.editBox.requestFocus();
  223.             } else if (bOsFlag && var1.target == this.editBox) {
  224.                this.dropList(false);
  225.             }
  226.             break;
  227.          case 1005:
  228.             if (var1.target == this.arrow && !this.bOverList) {
  229.                this.dropList(false);
  230.             }
  231.             break;
  232.          case 16388:
  233.             if (this.parent == null) {
  234.                this.parent = ((Component)this).getParent();
  235.             }
  236.  
  237.             if (this.parent != null) {
  238.                var1.target = this;
  239.                this.parent.postEvent(var1);
  240.             }
  241.  
  242.             return true;
  243.       }
  244.  
  245.       return super.handleEvent(var1);
  246.    }
  247.  
  248.    public static boolean addFirstTopDown() {
  249.       return !addLastBottomUp();
  250.    }
  251.  
  252.    public static boolean addLastBottomUp() {
  253.       String var0 = System.getProperty("os.name");
  254.       if (!var0.startsWith("S") && !var0.startsWith("Kona") && !var0.startsWith("AIX") && !var0.startsWith("OSF")) {
  255.          return var0.startsWith("Win") && System.getProperty("java.vendor").startsWith("Netscape") && System.getProperty("java.version").equals("1.02");
  256.       } else {
  257.          return true;
  258.       }
  259.    }
  260.  
  261.    public static boolean needsPlatformHelp() {
  262.       return bOsFlag;
  263.    }
  264.  
  265.    public static void unDropLastDropped() {
  266.       if (currentlyDown != null) {
  267.          currentlyDown.dropList(false);
  268.       }
  269.  
  270.    }
  271.  
  272.    public void clear() {
  273.       this.list.clear();
  274.       this.editBox.setText("");
  275.    }
  276.  
  277.    public synchronized void addItem(String var1) {
  278.       this.addItem((Image)null, var1, true);
  279.    }
  280.  
  281.    public synchronized void addItem(String var1, boolean var2) {
  282.       this.addItem((Image)null, var1, var2);
  283.    }
  284.  
  285.    public synchronized void addItem(Image var1, String var2) {
  286.       this.addItem(var1, var2, true);
  287.    }
  288.  
  289.    public synchronized void addItem(Image var1, String var2, boolean var3) {
  290.       this.list.addItem(var1, var2, var3);
  291.       this.arrow.enable();
  292.    }
  293.  
  294.    public void setListItems(String[] var1) {
  295.       this.clear();
  296.  
  297.       for(int var2 = 0; var2 < var1.length; ++var2) {
  298.          this.addItem(var1[var2]);
  299.       }
  300.  
  301.    }
  302.  
  303.    public String[] getListItems() {
  304.       int var1 = this.countItems();
  305.       String[] var2 = new String[var1];
  306.  
  307.       for(int var3 = 0; var3 < var1; ++var3) {
  308.          var2[var3] = this.getItem(var3);
  309.       }
  310.  
  311.       return var2;
  312.    }
  313.  
  314.    public int countItems() {
  315.       return this.list.countItems();
  316.    }
  317.  
  318.    public String getItem(int var1) {
  319.       return this.list.getItem(var1);
  320.    }
  321.  
  322.    public void changeImage(int var1, Image var2) {
  323.       this.list.changeImage(var1, var2);
  324.    }
  325.  
  326.    public void changeText(int var1, String var2) {
  327.       this.list.changeText(var1, var2);
  328.    }
  329.  
  330.    public void enable(int var1) {
  331.       this.list.enable(var1);
  332.    }
  333.  
  334.    public void disable(int var1) {
  335.       this.list.disable(var1);
  336.    }
  337.  
  338.    public void enable(int var1, boolean var2) {
  339.       if (var2) {
  340.          this.list.enable(var1);
  341.       } else {
  342.          this.list.disable(var1);
  343.       }
  344.    }
  345.  
  346.    public synchronized void delItem(int var1) {
  347.       this.delItems(var1, var1);
  348.    }
  349.  
  350.    public synchronized void delItems(int var1, int var2) {
  351.       this.list.delItems(var1, var2);
  352.       if (this.list.countItems() == 0) {
  353.          this.arrow.disable();
  354.          this.dropList(false);
  355.       }
  356.  
  357.    }
  358.  
  359.    public synchronized void delSelectedItem() {
  360.       this.list.delSelectedItems();
  361.    }
  362.  
  363.    public String getText() {
  364.       this.verify();
  365.       return this.editBox.getText();
  366.    }
  367.  
  368.    public String getSelectedItem() {
  369.       return this.list.getSelectedItem();
  370.    }
  371.  
  372.    public int getSelectedIndex() {
  373.       return this.list.getSelectedIndex();
  374.    }
  375.  
  376.    public synchronized void select(int var1) {
  377.       if (var1 >= 0 && var1 <= this.list.countItems()) {
  378.          this.list.select(var1);
  379.          this.editBox.setText(this.list.getSelectedItem());
  380.          if (bOsFlag) {
  381.             ((Component)this).repaint();
  382.          }
  383.       }
  384.  
  385.    }
  386.  
  387.    public void select(String var1) {
  388.       this.list.select(var1);
  389.       this.editBox.setText(this.list.getSelectedItem());
  390.       if (bOsFlag) {
  391.          ((Component)this).repaint();
  392.       }
  393.  
  394.    }
  395.  
  396.    public synchronized void deselect(int var1) {
  397.       this.list.deselect(var1);
  398.    }
  399.  
  400.    public synchronized boolean isSelected(int var1) {
  401.       return this.list.isSelected(var1);
  402.    }
  403.  
  404.    public synchronized void setFont(Font var1) {
  405.       this.editFont = this.dropFont = var1;
  406.       this.setEditFieldFont(var1);
  407.       this.setDropDownFont(var1);
  408.       super.setFont(var1);
  409.    }
  410.  
  411.    public void dropList(boolean var1) {
  412.       if (this.bDown != var1) {
  413.          this.bDown = var1;
  414.          if (var1) {
  415.             if (currentlyDown != null && currentlyDown != this) {
  416.                currentlyDown.dropList(false);
  417.             }
  418.  
  419.             currentlyDown = this;
  420.             this.list.show();
  421.             this.list.setDirty();
  422.             this.list.repaint();
  423.             if (((Component)this).getParent() != null) {
  424.                this.lheight = Math.min(((Component)this).getParent().bounds().height - this.field_1 - this.iheight - 10, this.list.minimumSize().height + 3);
  425.             } else {
  426.                this.lheight = this.list.minimumSize().height + 3;
  427.             }
  428.  
  429.             this.arrow.setDirection(2);
  430.          } else {
  431.             this.lheight = 0;
  432.             currentlyDown = null;
  433.             this.list.hide();
  434.             this.arrow.setDirection(3);
  435.             ((Component)this).invalidate();
  436.          }
  437.  
  438.          this.arrow.repaint();
  439.          super.reshape(this.field_0, this.field_1, this.iwidth, this.iheight + this.lheight);
  440.       }
  441.    }
  442.  
  443.    public synchronized void reshape(int var1, int var2, int var3, int var4) {
  444.       this.field_0 = var1;
  445.       this.field_1 = var2;
  446.       this.iwidth = var3;
  447.       if (bOsFlag) {
  448.          this.iheight = Math.max(29, var4);
  449.       } else {
  450.          this.iheight = var4;
  451.       }
  452.  
  453.       if (var4 > 22) {
  454.          var4 -= 22;
  455.          if (bOsFlag) {
  456.             var2 = 7;
  457.             var1 = 7;
  458.          } else {
  459.             var2 = 5;
  460.             var1 = 5;
  461.          }
  462.  
  463.          while(var4 > 0) {
  464.             ++var1;
  465.             --var4;
  466.             if (var4 > 0) {
  467.                ++var2;
  468.                --var4;
  469.             }
  470.          }
  471.  
  472.          this.arrow.shrinkTriangle(1, 1, var1, var2);
  473.       } else {
  474.          this.arrow.shrinkTriangle(1, 1, 5, 5);
  475.       }
  476.  
  477.       super.reshape(this.field_0, this.field_1, this.iwidth, this.iheight + this.lheight);
  478.    }
  479.  
  480.    public void paint(Graphics var1) {
  481.       super.paint(var1);
  482.       Dimension var2 = ((Component)this).size();
  483.       if (this.bDown) {
  484.          this.list.reshape(0, this.iheight + 1, var2.width, this.lheight);
  485.       }
  486.  
  487.       this.editBox.reshape(0, 0, var2.width - 16, this.iheight);
  488.       if (bOsFlag) {
  489.          this.arrow.reshape(var2.width - 16, 2, 15, this.iheight - 4);
  490.       } else {
  491.          this.arrow.reshape(var2.width - 16, 1, 15, this.iheight - 2);
  492.       }
  493.    }
  494.  
  495.    public Dimension preferredSize() {
  496.       Dimension var1 = ((Component)this).size();
  497.       Dimension var2 = this.minimumSize();
  498.       return new Dimension(Math.max(var1.width, var2.width), Math.max(var1.height, var2.height));
  499.    }
  500.  
  501.    public Dimension minimumSize() {
  502.       Dimension var1 = this.editBox.minimumSize();
  503.       var1.width += 17;
  504.       if (var1.height == 0) {
  505.          if (bOsFlag) {
  506.             var1.height = 29;
  507.          } else {
  508.             var1.height = 21;
  509.          }
  510.       }
  511.  
  512.       return var1;
  513.    }
  514.  
  515.    private void verify() {
  516.       if (this.bSearchable) {
  517.          this.searchIndex = this.editBox.getSelectionStart();
  518.          this.sSearchOrig = this.editBox.getText().substring(0, this.searchIndex);
  519.          if (this.bCaseSensitive) {
  520.             this.sSearchText = this.sSearchOrig;
  521.          } else {
  522.             this.sSearchText = this.sSearchOrig.toUpperCase();
  523.          }
  524.  
  525.          this.searchLen = this.list.countItems();
  526.          if (this.sSearchText.length() > 0) {
  527.             for(this.searchI = 0; this.searchI < this.searchLen; ++this.searchI) {
  528.                if (this.bCaseSensitive) {
  529.                   this.sSearchItem = this.list.getItem(this.searchI);
  530.                } else {
  531.                   this.sSearchItem = this.list.getItem(this.searchI).toUpperCase();
  532.                }
  533.  
  534.                if (this.sSearchItem.startsWith(this.sSearchText)) {
  535.                   this.editBox.setText(this.sSearchOrig);
  536.                   this.list.select(this.searchI);
  537.                   this.editBox.select(this.searchIndex, this.searchIndex);
  538.                   break;
  539.                }
  540.             }
  541.  
  542.             if (!this.bEditable && this.searchI == this.searchLen) {
  543.                this.editBox.setText(this.sLastText);
  544.                this.editBox.select(this.searchIndex - 1, this.searchIndex - 1);
  545.             }
  546.          }
  547.       }
  548.  
  549.       this.sLastText = this.editBox.getText();
  550.    }
  551. }
  552.